package cn.cheln.explorer.ds.scan;

import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import cn.cheln.support.highlight.Unicode2Alpha;

public class PinyinHelper {
	static HanyuPinyinOutputFormat hanyuPinyinOutputFormat;
	static {
		hanyuPinyinOutputFormat = new HanyuPinyinOutputFormat();
		hanyuPinyinOutputFormat.setCaseType(hanyuPinyinOutputFormat
				.getCaseType().LOWERCASE);
		hanyuPinyinOutputFormat.setVCharType(hanyuPinyinOutputFormat
				.getVCharType().WITH_V);
		hanyuPinyinOutputFormat.setToneType(hanyuPinyinOutputFormat
				.getToneType().WITHOUT_TONE);
	}

	public static String transfer(String name) {
		boolean isHead = true;
		String outpname = "";
		for (int i = 0; i < name.length(); i++) {
			String ch = new Character(name.charAt(i)).toString();
			String tranlated = "";
			try {
				tranlated = net.sourceforge.pinyin4j.PinyinHelper
						.toHanyuPinyinString(ch, hanyuPinyinOutputFormat, "");
			} catch (BadHanyuPinyinOutputFormatCombination e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (!ch.equals(tranlated)) {
				tranlated = "*" + tranlated + "*" + ch;
				if (isHead) {
					tranlated = tranlated.substring(1,2)+tranlated;
				}
			}
			isHead = false;
			outpname += tranlated;
		}
        String headSpell = Unicode2Alpha.toAlpha(name);
        if(!headSpell.equals(outpname)){
            outpname+="|"+ Unicode2Alpha.toAlpha(name);
        }
		return outpname;
	}
    public static String rawTransfer(String name) {
        String outpname = "";
        try {
            outpname = net.sourceforge.pinyin4j.PinyinHelper
                    .toHanyuPinyinString(name, hanyuPinyinOutputFormat, "");
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return outpname;
    }
}
